package com.gy.zpchat.ui.chat.group.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import im.zego.zim.entity.ZIMGroupMemberInfo;
import im.zego.zim.entity.ZIMGroupSearchInfo;
import com.gy.zpchat.R;
import com.gy.zpchat.enums.GroupRoleType;
import com.gy.zpchat.util.GlideUtils;
import java.util.ArrayList;
import java.util.List;

public class GroupSearchAdapter extends RecyclerView.Adapter {

    private List<ZIMGroupSearchInfo> groupSearchInfoList = new ArrayList<>();
    private OnItemClickListener onItemClickListener;
    private GroupRoleType groupRole;

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder =
            new GroupInfoHolder(LayoutInflater.from(parent.getContext())
                                    .inflate(R.layout.item_search_group_info, parent, false));
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        GroupInfoHolder infoHolder = (GroupInfoHolder)holder;
        infoHolder.setSearchInfo(groupSearchInfoList.get(position));
    }

    @Override
    public int getItemCount() {
        return groupSearchInfoList.size();
    }

    public void addOrUpdate(ZIMGroupSearchInfo info) {
        for (int i = 0; i < groupSearchInfoList.size(); i++) {
            ZIMGroupSearchInfo oldO = groupSearchInfoList.get(i);
            if (info.groupInfo.groupID.equals(oldO.groupInfo.groupID)) {
                groupSearchInfoList.set(i, info);
                return;
            }
        }
        groupSearchInfoList.add(info);
    }

    public void setGroupRole(GroupRoleType role) { this.groupRole = role; }

    public interface OnItemClickListener {
        void onItemClick(GroupMemberUpdateType type);

        void onItemInfoClick(ZIMGroupMemberInfo info);
    }

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.onItemClickListener = mOnItemClickListener;
    }

    protected class GroupInfoHolder extends RecyclerView.ViewHolder {

        public TextView titleTextView;
        public ImageView headerImageVIew;
        public RelativeLayout relativeLayout;
        public TextView groupMemberCountTextView;

        public GroupInfoHolder(@NonNull View itemView) {
            super(itemView);
            titleTextView = itemView.findViewById(R.id.title);
            headerImageVIew = itemView.findViewById(R.id.header);
            relativeLayout = itemView.findViewById(R.id.relativeLayout);
            groupMemberCountTextView = itemView.findViewById(R.id.groupMemberCount);
        }

        public void setSearchInfo(ZIMGroupSearchInfo info) {
            String title = info.groupInfo.groupName;
            if (info.groupInfo.groupName != null && !info.groupInfo.groupName.equals("")) {
                title = info.groupInfo.groupName;
            } else if (info.groupInfo.groupID != null) {
                title = info.groupInfo.groupID;
            }
            titleTextView.setText(title);
            groupMemberCountTextView.setText("匹配群成员人数:" + info.userList.size());
            GlideUtils.loadUrl(headerImageVIew, info.groupInfo.groupAvatarUrl);
        }
    }

    public enum GroupMemberUpdateType {
        GROUP_MEMBER_UPDATE_TYPE_ADD,
        GROUP_MEMBER_UPDATE_TYPE_Delete
    }
}
